﻿using System;
using System.IO;

namespace XiaoSe.IO
{
    /// <summary>
    /// 文本类型文件帮助类
    /// </summary>
    public class TextFileHelper
    {
        /// <summary>
        /// 成功返回文件内容，失败返回null
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>字符串</returns>
        public string ReadFile(string filePath)
        {
            string content;
            try
            {
                var sr = new StreamReader(filePath);
                content = sr.ReadToEnd();
                sr.Close();
            }
            catch
            {
                content = "";
            }
            return content;
        }

        /// <summary>
        /// 将内容写到指定的文件中
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">文件内容</param>
        /// <param name="encoding"></param>
        /// <returns>布尔值</returns>
        public bool WriteFile(string filePath, string content, System.Text.Encoding encoding)
        {
            if (content == null) throw new ArgumentNullException("content");
            try
            {
                if (!File.Exists(filePath))
                {
                    File.Create(filePath).Close();
                }
                var sw = new StreamWriter(filePath, false, encoding);
                var arraycontent = content.ToCharArray();
                sw.Write(arraycontent, 0, arraycontent.Length);
                sw.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 将内容追加到指定文件的后面
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">要追加的内容</param>
        /// <returns>布尔值</returns>
        public bool AppendText(string filePath, string content)
        {
            try
            {
                byte[] byteContent = System.Text.Encoding.UTF8.GetBytes(content.ToCharArray());
                var fs = new FileStream(filePath, FileMode.Append);
                fs.Write(byteContent, 0, byteContent.Length);
                fs.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 截断字符串
        /// </summary>
        /// <param name="str">需要截断的字符串</param>
        /// <param name="length">长度</param>
        /// <returns>返回截断后的字符串</returns>
        public static string CutString(string str, int length)
        {
            return str.Length > length ? str.Substring(0, length) : str;
        }

        /// <summary>
        /// 字符转换
        /// </summary>
        /// <param name="content"></param>
        /// <param name="vbstrconv"></param>
        /// <returns></returns>
        public static string ChineseExchange(string content, Microsoft.VisualBasic.VbStrConv vbstrconv)
        {
            content = Microsoft.VisualBasic.Strings.StrConv(content, vbstrconv);
            return content;
        }
    }
}
